#include "view_time_3.inc"
#include "view_time_3.h"
#include "view_time_1.h"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#ifdef TIME_3_SKIP
#else

static void view_time_3_time(void)
{
	int img_time_3_time_hour_num_TAB[] = 
	{
		FILE_IMG_data4_font_hour_4_18_0,
		FILE_IMG_data4_font_hour_4_18_1,
		FILE_IMG_data4_font_hour_4_18_2,
		FILE_IMG_data4_font_hour_4_18_3,
		FILE_IMG_data4_font_hour_4_18_4,
		FILE_IMG_data4_font_hour_4_18_5,
		FILE_IMG_data4_font_hour_4_18_6,
		FILE_IMG_data4_font_hour_4_18_7,
		FILE_IMG_data4_font_hour_4_18_8,
		FILE_IMG_data4_font_hour_4_18_9,
	};
	
	int img_time_3_time_minute_num_TAB[] = 
	{
		FILE_IMG_data4_font_min_4_69_0,
		FILE_IMG_data4_font_min_4_69_1,
		FILE_IMG_data4_font_min_4_69_2,
		FILE_IMG_data4_font_min_4_69_3,
		FILE_IMG_data4_font_min_4_69_4,
		FILE_IMG_data4_font_min_4_69_5,
		FILE_IMG_data4_font_min_4_69_6,
		FILE_IMG_data4_font_min_4_69_7,
		FILE_IMG_data4_font_min_4_69_8,
		FILE_IMG_data4_font_min_4_69_9,
	};
	
	int x = 6;
	int y = 18;
	int front_width = 34;
	int front_height = 46;
	int front_width_spacing = 5;
	int front_height_spacing = 5;
	_disp_show_flash_img(DISP_LFS_FILE_INDEX_0,x,y,img_time_3_time_hour_num_TAB[disp_ctrl.com_data.i_temp[3]/10]);
	_disp_show_flash_img(DISP_LFS_FILE_INDEX_1,x+front_width+front_width_spacing,y,img_time_3_time_hour_num_TAB[disp_ctrl.com_data.i_temp[3]%10]);
	_disp_show_flash_img(DISP_LFS_FILE_INDEX_2,x,y+front_height_spacing+front_height,img_time_3_time_minute_num_TAB[disp_ctrl.com_data.i_temp[4]/10]);
	_disp_show_flash_img(DISP_LFS_FILE_INDEX_3,x+front_width+front_width_spacing,y+front_height_spacing+front_height,img_time_3_time_minute_num_TAB[disp_ctrl.com_data.i_temp[4]%10]);
}

static void view_time_3_steps(void)
{
	const uint8_t img_time_3_step_schedule[]=
	{
		FILE_IMG_data4_icon_steps_10_116_1,
		FILE_IMG_data4_icon_steps_10_116_2,
		FILE_IMG_data4_icon_steps_10_116_3,
		FILE_IMG_data4_icon_steps_10_116_4,
		FILE_IMG_data4_icon_steps_10_116_5,
		FILE_IMG_data4_icon_steps_10_116_6,
		FILE_IMG_data4_icon_steps_10_116_7,
		FILE_IMG_data4_icon_steps_10_116_8,
		FILE_IMG_data4_icon_steps_10_116_9,
		FILE_IMG_data4_icon_steps_10_116_10,
	};
	int steps = disp_ctrl.com_data.i_temp[11];
	int schedule_x = 10;
	int schedule_y = 116;
	if(steps == 0)
	{
		_disp_show_flash_img(DISP_LFS_FILE_INDEX_4,schedule_x,schedule_y,FILE_IMG_data4_icon_steps_10_116_0);
	}
	else
	{
		int schedule = steps /8000;
		if(schedule>=9)
		{
			schedule =9;
		}
		_disp_show_flash_img(DISP_LFS_FILE_INDEX_4,schedule_x,schedule_y,img_time_3_step_schedule[schedule]);
	}
	disp_draw_user_image(43,176,img_time_3_step);
	disp_draw_user_image_num_mid_gray_lucency(59,144,RGB565_COLOR_WHITE,steps,10,img_time_1_step_num_TAB);
}

static void view_time_3_distance(void)
{
	float distance = 0.0f;
	if (system_config_get_km_mi() != 0)
  {
		
		distance = sport_get_distance() * 0.6213712;
    disp_ctrl.com_data.i_temp[32] = 0;
  }
  else
  {
    distance = sport_get_distance();
    disp_ctrl.com_data.i_temp[32] = 1;
  }
	
	int distance_schedule = distance/5;
	if(distance <= 1)
	{
		disp_draw_user_image_filter_color(78,18,img_time_3_schedule_last);
	}
	else
	{
		if(distance_schedule>=9)
		{
			distance_schedule = 9;
		}
		disp_draw_user_image_filter_color(78,18,img_time_3_schedule_TAB[distance_schedule]);
	}
	disp_draw_user_image_filter_color(79,16,img_time_3_distance);
}

static void view_time_3_kcal(void)
{
	int kcal = disp_ctrl.com_data.i_temp[16];
	int kcal_schedule = kcal/250;
	if(kcal <= 1000)
	{
		disp_draw_user_image(78,69,img_time_3_schedule_last);
	}
	else
	{
		if(kcal_schedule >= 9)
		{
			kcal_schedule = 9;
		}
		disp_draw_user_image_filter_color(78,69,img_time_3_schedule_TAB[kcal_schedule]);
	}
	disp_draw_user_image_filter_color(79,65,img_time_3_kcal);
}

void view_time_3_display(void)
{
	view_time_3_time();
	view_time_1_ble(disp_ctrl.com_data.i_temp[7],7,5);
  view_time_1_battery();
	view_time_3_distance();
	view_time_3_kcal();
	view_time_3_steps();
}
#endif
